#Replication code for Study 1 (NLC) in 'How does Partisanship Influence Policy Diffusion?'
#Authors: Daniel Butler and Miguel Pereira.
#Note: if you have any questions, please contact Miguel Pereira (m.pereira@wustl.edu).

rm(list=ls())
library(foreign)
library(lmtest)

setwd('INSERT PATH HERE')
nlc.data <- read.csv("nlc_data.csv",head = TRUE)


################################################### 
## TABLE 1 - Descriptives 
###################################################

#Age
mean(na.omit(nlc.data$age))  
#Gender
prop.table(table(nlc.data$gender))  
#Bachelor+
prop.table(table(nlc.data$educ))[5]+prop.table(table(nlc.data$educ))[6]
#Elected
prop.table(table(nlc.data$elected))
#Partisanship
prop.table(table(nlc.data$pid.new)) 
#Partisan elections
prop.table(table(nlc.data$nonpart_el))


################################################### 
## TABLE 2 - Copartisan effects NLC 
###################################################
t.test(nlc.data$policy.interest[nlc.data$tr.copartisan2==1],nlc.data$policy.interest[nlc.data$tr.copartisan2==0])
length(na.omit(nlc.data$policy.interest[nlc.data$tr.copartisan2==1 | nlc.data$tr.copartisan2==0]))


################################################### 
## TABLE 4 - Independents (NLC column) 
###################################################
t.test(nlc.data$policy.interest[nlc.data$pid.new=='Ind'&nlc.data$Republican_treatment==0],
       nlc.data$policy.interest[nlc.data$pid.new=='Ind'&nlc.data$Republican_treatment==1])
length(na.omit(nlc.data$policy.interest[nlc.data$pid.new=='Ind']))
se.nlc.inds<-sqrt(sd(na.omit(nlc.data$policy.interest[nlc.data$pid.new=='Ind'&nlc.data$Republican_treatment==0]))^2 / length(na.omit(nlc.data$policy.interest[nlc.data$pid.new=='Ind'&nlc.data$Republican_treatment==0])) + 
                    sd(na.omit(nlc.data$policy.interest[nlc.data$pid.new=='Ind'&nlc.data$Republican_treatment==1]))^2 / length(na.omit(nlc.data$policy.interest[nlc.data$pid.new=='Ind'&nlc.data$Republican_treatment==1])))


################################################### 
## TABLE A1 - Participants by state
###################################################
table(nlc.data$state)
#Note: State abbreviations in alphabetical order


################################################### 
## TABLE A2 - Covariate Balance
###################################################

nlc.data$female=nlc.data$gender-1
nlc.data$bachelor<-ifelse(nlc.data$educ>4,1,0)
nlc.data$elec=nlc.data$elected-1
nlc.data$dem<-ifelse(nlc.data$pid.new=='Dem',1,0)
nlc.data$rep<-ifelse(nlc.data$pid.new=='Rep',1,0)
nlc.data$nonpart_el<-nlc.data$nonpart_el-1

#Covariate Balance
#Age
mean(na.omit(nlc.data$age[nlc.data$tr.copartisan2==1]))  
mean(na.omit(nlc.data$age[nlc.data$tr.copartisan2==0]))  
summary(aov(age~tr.copartisan2,nlc.data))
#Gender
mean(na.omit(nlc.data$female[nlc.data$tr.copartisan2==1]))  
mean(na.omit(nlc.data$female[nlc.data$tr.copartisan2==0]))  
summary(aov(female~tr.copartisan2,nlc.data))
#Bachelor+
mean(na.omit(nlc.data$bachelor[nlc.data$tr.copartisan2==1]))  
mean(na.omit(nlc.data$bachelor[nlc.data$tr.copartisan2==0]))  
summary(aov(bachelor~tr.copartisan2,nlc.data))
#Elected
1-mean(na.omit(nlc.data$elec[nlc.data$tr.copartisan2==1]))  
1-mean(na.omit(nlc.data$elec[nlc.data$tr.copartisan2==0]))  
summary(aov(elec~tr.copartisan2,nlc.data))
#Democrat
mean(na.omit(nlc.data$dem[nlc.data$tr.copartisan2==1]))  
mean(na.omit(nlc.data$dem[nlc.data$tr.copartisan2==0]))  
summary(aov(dem~tr.copartisan2,nlc.data))
#Non-partisan elections
mean(na.omit(nlc.data$nonpart_el[nlc.data$tr.copartisan2==1]))  
mean(na.omit(nlc.data$nonpart_el[nlc.data$tr.copartisan2==0]))  
summary(aov(nonpart_el~tr.copartisan2,nlc.data))

#Likelihood Ratio Test
css<-complete.cases(nlc.data[,c('tr.copartisan2','age','female',
                                'bachelor', 'elec', 'dem', 'nonpart_el')])
summary(glm(tr.copartisan2~age+female+bachelor+elec+dem+nonpart_el
            , family='binomial',nlc.data))
mod1<-glm(tr.copartisan2~1, family='binomial',nlc.data[css,])
mod2<-glm(tr.copartisan2~age+female+bachelor+elec+dem+nonpart_el, family='binomial',nlc.data[css,])
lrtest(mod2,mod1)


################################################### 
## TABLE A6 - Distribution of responses
###################################################
#Proportion of response in each category, by treatment condition
(pt_nlc<-round(prop.table(table(nlc.data$tr.copartisan2,
                               nlc.data$policy.interest),1),2))
#Chi-square test
ct_nlc<-table(nlc.data$tr.copartisan2,
              nlc.data$policy.interest)
chisq.test(ct_nlc)
